<?php
	require_once '../query/config.php';
	require_once '../html/html.php';
	$contract = $_REQUEST['contract'];
	if ( isset($contract) && $contract == "add" ){
		$conName 		= $_REQUEST['conName'];
		$conStartDate 	= $_REQUEST['conStartDate'];
		$conEndDate		= $_REQUEST['conEndDate'];
		$description	= $_REQUEST['description'];
		$user_id 		= $_REQUEST['customer'];
		$id 			= $_REQUEST['id'];
		$cdate			=  $mdate = date('Y-m-d');				
		$publish 	= 1;
		
		new dbConnect();
		if ($id == 0){
			$query = "INSERT INTO `tcontract` SET `con_name` = '".$conName."', `con_datestart` = '".$conStartDate."',`con_dateend` = '".$conEndDate."'".
					", `description` = '".$description."', `user_id`='". $user_id ."', `cdate` = '".$cdate."', `publish` = $publish";
//			echo $query;	
			if (mysql_query($query)){
				echo mysql_insert_id();
			}else{
				echo "0";
			}
		}else{			
			$query = "UPDATE `tcontract` SET `con_name` = '".$conName."', `con_datestart` = '".$conStartDate."',`con_dateend` = '".$conEndDate."'".
					", `description` = '".$description."', `user_id`='". $user_id ."', `mdate` = '".$mdate."' , `publish` = $publish  WHERE `id`='".$id."'";
//			echo $query; 	
			if (mysql_query($query)){
				echo $id;
			}else{
			  	echo "0";
			}
		} 
	}elseif( isset($contract) && $contract == "getContract" ){
		$id 		= $_REQUEST['id'];
		new dbConnect();
		$query = "SELECT * FROM `tcontract` WHERE publish = 1 and id = ".$id;
		$res = mysql_query($query);
		echo json_encode(mysql_fetch_object($res));		
	}elseif( isset($contract) && $contract == 'showByCustomer' ){
		$idCustomer		= $_REQUEST['idCustomer'];
		new dbConnect();
		$query = "SELECT tc.* FROM `tcontract` as tc WHERE tc.publish = 1 and tc.user_id = ".$idCustomer;
		$res = mysql_query($query);
		while ($rows = mysql_fetch_object($res)){
			$lists[] = $rows; 
		}		
		echo ajaxHTML::contractForm($lists);
	}
?>